import './login.css'
import React,{useState} from 'react'
import axios from 'axios'

function Login(props){
    const [json,setjson] = useState({username:'',password:''})

    function getval(event){
        let dom = event.target
        let type = dom.getAttribute('login')
        let val = dom.value.split(' ').join('')
        let newjson = {...json}
        newjson[type] = val
        setjson(newjson)
    }
    function btn(){
        if(json.username === ''||json.password ===''){
            alert('请输入内容')
        }else{
            axios.post('http://crm.cimns.com/index.php/admin/base/login',json).then((data)=>{
                console.log(data);
                if(data.data.code === 200){
                    localStorage.login = JSON.stringify(data.data.data)
                    props.history.push('/App')
                    console.log('登录成功');
                }else if(data.data.code === 400){
                    alert('登录失败')
                }
            })
        }
    }
    return (
        <div className="clearfix">
            <div className = 'bj'>
                <img alt='000' src="http://crm.cimns.com/static/img/logo.3e34073.png"/>
            </div>
            <div className='dl'>
                <p>悟空CRM</p>
                <div>
                    <input type="username" login="username" value={json.username} onChange={getval} placeholder="请输入账号"/>
                    <input type="password" login="password" value={json.password} onChange={getval} placeholder="请输入密码"/>
                </div>
                <button onClick={btn}>登录</button>
            </div>
        </div>
    )
}

export default Login